package gui.NetworkManager;

import java.awt.Color;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;
import javax.swing.text.JTextComponent;

import control.LoginController;
import control.ReportsController;

import system.SystemMode;
import system.SystemTypes;
import system.VCP;
import javax.swing.JComboBox;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import com.toedter.calendar.JDateChooser;
/**
 * this class use as Report Menu Screen
 * @author G8
 *
 */
public class ReportMenuScreen extends JPanel implements ActionListener{
	public JButton btnLogout;
	public JButton btnBack;
	private JDateChooser textFieldDate;
	private JComboBox comboBoxType;
	private JComboBox comboBoxLot;
	private JButton btnView;
	private SystemTypes reportToGet;
	private ReportsController reportsController;
	private JTextComponent textAreaReportContent;
	/**
	 * Report Menu Screen Constructor 
	 */
	public ReportMenuScreen(){
		super();
		initialize();
	}
	/**
	 * This method contains the view of the Panel
	 */
	private void initialize() {
		reportsController = new ReportsController();
		setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Reports Menu", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		setBackground(SystemColor.menu);
		setBounds(VCP.guiX,VCP.guiY,500,400);
		setLayout(null);
		VCP.frame.setBounds(this.getBounds());
		
		btnBack = new JButton("Back");
		btnBack.setIcon(new ImageIcon(ReadOnlyDataScreen.class.getResource("/img/back.png")));
		btnBack.setSize(150, 30);
		btnBack.setBounds(281, 22, 102, 35);
		btnBack.addActionListener(this);
		add(btnBack);
		
		btnLogout = new JButton("Logout");
		btnLogout.setSize(150, 30);
		btnLogout.setBounds(393, 22, 78, 35);
		btnLogout.addActionListener(this);
		add(btnLogout);
		
		JLabel lblReportType = new JLabel("Report Type:");
		lblReportType.setBounds(69, 71, 102, 14);
		add(lblReportType);
		
		comboBoxType = new JComboBox();
		comboBoxType.setBounds(181, 68, 202, 20);
		comboBoxType.addItem("Quarter Report");
		comboBoxType.addItem("Activity Report");
		comboBoxType.addItem("Performance Report");
		comboBoxType.addItem("Exception Report");
		comboBoxType.addActionListener(this);
		add(comboBoxType);
		
		textFieldDate = new JDateChooser();
		textFieldDate.setBounds(181, 96, 114, 20);
		textFieldDate.setEnabled(false);
		add(textFieldDate);
		
		JLabel lblReportContent = new JLabel("Content:");
		lblReportContent.setBounds(69, 147, 92, 14);
		add(lblReportContent);
		
		textAreaReportContent = new JTextArea();
		textAreaReportContent.setEditable(false);
		JScrollPane scroll = new JScrollPane (textAreaReportContent, 
		JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		scroll.setBounds(69, 172, 365, 173);
		add(scroll);
		
		JLabel lblDate = new JLabel("Report Date:");
		lblDate.setBounds(69, 96, 92, 14);
		add(lblDate);
		
		JLabel lblLot = new JLabel("Lot:");
		lblLot.setBounds(69, 121, 46, 14);
		add(lblLot);
		
		comboBoxLot = new JComboBox();
		comboBoxLot.setEnabled(false);
		comboBoxLot.setBounds(181, 118, 114, 20);
		for (int i=1;i<7;i++)
			comboBoxLot.addItem(i);
		comboBoxLot.addActionListener(this);
		add(comboBoxLot);
		
		btnView = new JButton("View");
		btnView.setBounds(305, 100, 78, 35);
		btnView.addActionListener(this);
		add(btnView);
	}
	/**
	 * This method listens to the buttons on the panel
	 * @param e is the buttons the user Click
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == comboBoxType){
			if (!comboBoxType.getSelectedItem().equals("Activity Report")){
				textFieldDate.setEnabled(false);
				comboBoxLot.setEnabled(false);
			}
			if (comboBoxType.getSelectedItem().equals("Activity Report")){
				textFieldDate.setEnabled(true);
				comboBoxLot.setEnabled(true);
			}
		}
		if (e.getSource() == btnView){
			if (comboBoxType.getSelectedItem().equals("Performance Report")){
				reportToGet = new SystemTypes("Performance Report",null);
			}
			else if (comboBoxType.getSelectedItem().equals("Exception Report")){
				reportToGet = new SystemTypes("Exception Report",null);
			}
			SystemTypes reportResult = reportsController.GenerateReport(reportToGet);
			if (reportResult.getSysMode().equals(SystemMode.OK))
				textAreaReportContent.setText(reportResult.getObj().toString());
			else
				JOptionPane.showMessageDialog(null, reportResult.getObj().toString(), "Error",
	                    JOptionPane.ERROR_MESSAGE);
		}
		if (e.getSource() == btnBack){
			VCP.setScreen(this, VCP.prevScreen);
			VCP.prevScreen.setBounds(VCP.guiX,VCP.guiY,500,400);
			VCP.prevScreen.setLayout(null);
			VCP.frame.setBounds(VCP.prevScreen.getBounds());
		}
		if (e.getSource() == btnLogout){
			SystemTypes userToLogout = new SystemTypes(VCP.currentUser,null);
			boolean logoutRes = LoginController.LogOut(userToLogout);
			if (logoutRes)
				VCP.setGUI(0, this);
			else{
				JOptionPane.showMessageDialog(null, "Failed to logout!", "Error",
	                    JOptionPane.ERROR_MESSAGE);
			}
		}
	}
}
